<template>
	<view class="">
		<nav-bar title="商品结算"></nav-bar>
		<van-contact-card type="edit" :tel="address.tel" :name="address.name" @click="goAddres" />

		<van-card v-for="(c,ci) in payGoods" @click="goGoodsDetail(c._id)" :price="c.price" :desc="c.goods_desc"
			:title="c.name" :thumb="c.goods_thumb" :origin-price="c.pre_price">
			<template #num>
				<view class="cart-num">
					<view class="cart-num-btn">{{c.num}}</view>
				</view>
			</template>
		</van-card>
		<view class="">
			{{total}}
		</view>
		<view class="pay-btn">
			<van-button type="warning" @click="QBPay()">钱包支付</van-button>
			<van-button type="success">微信支付</van-button>
		</view>
	</view>
</template>

<script>
	import Storage from '@/untils/storage.js'
	const User = uniCloud.importObject('user')
	const Order = uniCloud.importObject('order')
	export default {
		data() {
			return {
				payGoods: []
			}
		},
		computed: {
			address() {
				if (this.$store.state.address.defaultAddress) {
					this.$store.commit("storageToDefaultAddress", Storage.get("defaultAddress"))
				}
				return this.$store.state.address.defaultAddress
			},
			total() {
				let total = 0;
				for (var i = 0; i < this.payGoods.length; i++) {
					let goods = this.payGoods[i];
					total += goods.price * goods.num
				}
				return total;
			}
		},
		onLoad() {
			this.payGoods = Storage.get("payGoods")
		},
		methods: {
			goAddres() {
				uni.navigateTo({
					url: "/pages/user/address/list"
				})
			},
			QBPay() {
				let that = this;
				//登录超期了 拿不到user_id
				User.money(Storage.userinfo._id).then(res => {
					if (res.money > this.total) {
						return res;
					} else {
						uni.showModal({
							title: "余额不足",
							content: `您的当前余额为${res.money/100}元，不足购买该商品，请充值或者换成其他支付方式`
						})
					}
				}).then(res=>{
					uni.showModal({
						title: "确认支付",
						content: `您的当前余额为${res.money/100}元，支付后还剩${(res.money-this.total)/100}元`,
						success(sres) {
							if (sres.confirm) {
								Order.QBPay(that.payGoods, Storage.userinfo._id, that.address,this.total).then(payRes => {
									if(payRes.success){
										//清空缓存中的商品 无返回跳转到订单页面
										Storage.set("payGoods",[])
										uni.redirectTo({
											url:"/pages/order/list"
										})
									}
								})
							}
						}
					})
				})
	        }
	    }
	};
</script>

<style>
	.pay-btn {
		width: 700upx;
		margin: auto;
		display: flex;
		flex-direction: column;
	}

	.pay-btn .van-button {
		margin: 10upx 0;
	}
</style>
